<template>
  <view>
    <view>
      <u-form :model="queryParams" ref="uForm">
        <u--input
            placeholder="请输入工单号"
            border="surround"
            v-model="queryParams.woCode"
            @change="queryWorks"
        ></u--input>
        <u-form-item label="工单" v-if="tempShow">
          <uni-data-select v-model="tempWoCode" placeholder="请选择工单" clearable
                           :localdata="woCodes" @change="changeWoCode"/>
        </u-form-item>
        <u-form-item label="导线">
          <u-switch v-model="queryParams.indivisible" active-color="red" style="margin-right: 5px;"/>
        </u-form-item>
        <u-button @click="search">搜索</u-button>
      </u-form>
    </view>

      <view class="button-container">
        <u-button @click="addLoadDatas">上料</u-button>
      </view>

      <view>
        <view style="display: flex; align-items: center;">
          <u--text text="序号" type="success"></u--text>
          <u--text text="物料编码" type="success"></u--text>
          <u--text text="物料名称" type="success"></u--text>
        </view>
        <u-list @scrolltolower="scrolltolower">
          <u-list-item v-for="(item, index) in loadMaterialList" :key="index">
            <view style="display: flex; align-items: center;">
              <u-cell :title="`${index + 1}`"></u-cell>
              <u-cell :title="`${item.invCode}`"></u-cell>
              <u-cell :title="`${item.invName}`"></u-cell>
            </view>
          </u-list-item>
        </u-list>
      </view>
  </view>
</template>


<script>
import UInput from "../../../../uview-ui/components/u--input/u--input.vue";
import UForm from "../../../../uview-ui/components/u--form/u--form.vue";
import UFormItem from "../../../../uview-ui/components/u-form-item/u-form-item.vue";
import UniDataSelect from "../../../../uni_modules/uni-data-select/components/uni-data-select/uni-data-select.vue";
import {addLoadMaterials, getWorks, listLoadMaterial} from "../../../../api/work/productLine/load";
import UTable from "../../../../uview-ui/components/u-table/u-table.vue";
import UTr from "../../../../uview-ui/components/u-tr/u-tr.vue";
import UTd from "../../../../uview-ui/components/u-td/u-td.vue";
import UListItem from "../../../../uview-ui/components/u-list-item/u-list-item.vue";
import UCell from "../../../../uview-ui/components/u-cell/u-cell.vue";
import UAvatar from "../../../../uview-ui/components/u-avatar/u-avatar.vue";
import UList from "../../../../uview-ui/components/u-list/u-list.vue";
import UButton from "../../../../uview-ui/components/u-button/u-button.vue";
import USwitch from "../../../../uview-ui/components/u-switch/u-switch.vue";
import UText from "../../../../uview-ui/components/u--text/u--text.vue";
import UTag from "../../../../uview-ui/components/u-tag/u-tag.vue";
import UToast from "../../../../uview-ui/components/u-toast/u-toast.vue";

export default {
  data() {
    return {
      woCodes: [{value: '测试', text: '测试'}],
      tempWoCode: null,
      tempShow: false,
      queryParams: {
        woCode: null,
        indivisible: false,
        pageNum: 1,
        pageSize: 20,
      },
      show: true,
      total: 0,
      // 快速上料表格数据
      loadMaterialList: [],
      loading: true,
    }
  },
  methods: {
    search() {
      // 校验搜索条件
      if (this.checkObjectEmpty(this.queryParams)) {
        this.loading = false;
        if (this.notFirstGetList) {
          this.$modal.msgError("请输入查询条件");
          return;
        } else {
          this.notFirstGetList = true;
          return;
        }
      }
      this.queryParams.pageNum = 1;
      // 查询数据
      listLoadMaterial(this.queryParams).then(response => {
        this.loadMaterialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    checkObjectEmpty(obj) {
      for (let key in obj) {
        if (key === 'pageNum' || key === 'pageSize') {
          continue;
        }
        if (obj.hasOwnProperty(key) && obj[key] !== null && obj[key] !== undefined && obj[key] !== '' && obj[key] !== false) {
          return false;
        }
      }
      return true;
    },
    queryWorks(query) {
      this.woCodes = [];
      getWorks(query).then(response => {
        this.woCodes = response.data.map((data) => {
          return {
            value: data,
            text: data,
          }
        });
        this.tempShow = true;
      });
    },
    changeWoCode() {
      this.tempShow = false;
      this.queryParams.woCode = this.tempWoCode;
    },
    scrolltolower() {
      if (this.loadMaterialList.length >= this.total) {
        this.$modal.msgError("没有更多数据了");
        return;
      }
      this.queryParams.pageNum = this.queryParams.pageNum + 1;
      listLoadMaterial(this.queryParams).then(response => {
        response.rows.forEach(row => this.loadMaterialList.push(row))
        console.log(this.loadMaterialList);
        this.total = response.total;
        this.loading = false;
      });
    },
    addLoadDatas() {
      if (this.queryParams.woCode === null || this.queryParams.woCode === '') {
        this.$modal.msgError("工单号不能为空");
        return;
      }
      let self = this;
      this.$modal.confirm('是否对工单为：'+this.queryParams.woCode + '进行上料').then(function () {
        addLoadMaterials(self.queryParams).then((res) => {
          if (res.code !== 200) {
            if (res.msg !== null && res.msg !== undefined) {
              self.$modal.msgError(res.msg);
            } else {
              self.$modal.msgError("后端接口异常");
            }
          } else {
            self.$modal.msgSuccess("上料成功");
            self.search();
          }

        }).catch(function (error) {
          // 处理接口返回的错误
          this.$modal.msgError("后端接口异常");
        });
      }).catch(function () {
        // 用户取消确认的操作
      });
    },
  },
  components: {
    UToast,
    UTag,
    UText,
    USwitch,
    UButton, UList, UAvatar, UCell, UListItem, UTd, UTr, UTable, UniDataSelect, UInput, UFormItem, UForm
  }
};
</script>
<style scoped>
.button-container {
  display: flex;
  justify-content: space-between;
}
</style>
